import Service from "./service";
import { defaultFormData, useModel } from "./model";
import { createVNode, onMounted } from "vue";
import { message, Modal } from "ant-design-vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";

export const usePresenter = () => {
  const model = useModel();
  const service = new Service(model);

  onMounted(() => {
    service.<%= serviceName %>();
  });

  const handleClear = () => {
		Object.assign(model.filterForm, defaultFormData)
		<% if(pagination.show) { _%>
			model.pagination.page = 1;
		<% } _%>
    service.<%= serviceName %>();
  };

  const handleSearch = () => {
		<% if(pagination.show) { _%>
			model.pagination.page = 1;
		<% } _%>
    service.<%= serviceName %>();
  };

  <% if(pagination.show) { _%>
  const handlePageChange = (page: number, pageSize: number) => {
    if (pageSize !== model.pagination.pageSize) {
      model.pagination.pageSize = pageSize;
      model.pagination.page = 1;
    } else {
      model.pagination.page = page;
    }
    service.<%= serviceName %>();
  };
  <% } _%>

  const handleDel = (record: typeof model.tableList.value[0]) => {
    Modal.confirm({
      title: "此操作将删除该选项，是否继续？",
      icon: createVNode(ExclamationCircleOutlined),
			okText: "确定",
      cancelText: "取消",
      onOk() {
        message.success("删除成功");
      },
    });
  };

  const handleCreate = () => {
	<% if(includeModifyModal) { _%>
    model.modalInfo.visible = true;
    model.modalInfo.title = "新建";
    model.modalInfo.action = "add";
    model.modalInfo.id = undefined;
	<% } _%>
  };

  const handleEdit = (record: typeof model.tableList.value[0]) => {
	<% if(includeModifyModal) { _%>
    model.modalInfo.visible = true;
    model.modalInfo.title = "编辑";
    model.modalInfo.action = "edit";
    model.modalInfo.id = record.id;
	<% } _%>
  };

  <% if(includeModifyModal) { _%>
  const handleView = (record: typeof model.tableList.value[0]) => {
    model.modalInfo.visible = true;
    model.modalInfo.title = "查看";
    model.modalInfo.action = "view";
    model.modalInfo.id = record.id;
  };

  const handleModalOk = () => {
    model.modalInfo.visible = false;
    service.<%= serviceName %>();
  };

  const handleModalCancel = () => {
    model.modalInfo.visible = false;
  };
  <% } _%>

  return {
    model,
    service,
    handleClear,
    handleSearch,
	<% if(pagination.show) { _%>
	handlePageChange,
    <% } _%>
	handleDel,
	handleCreate,
    handleEdit,
	<% if(includeModifyModal) { _%>
	handleView,
	handleModalOk,
	handleModalCancel
	<% } _%>
  };
};
